package com.cloudlead.service.impl;

import com.cloudlead.common.lang.CommonBusinessModules;
import com.cloudlead.common.lang.service.AbstractBaseService;
import com.cloudlead.service.FileUploadService;
import com.cloudlead.utils.oss.FileContent;
import com.cloudlead.utils.oss.FileUpload;
import com.cloudlead.utils.oss.FileUploadClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;

/**
 * Created by Dean on 2015/4/30.
 */
@Service("FileUploadService")
public class FileUploadServiceImpl extends AbstractBaseService implements FileUploadService {
    @Value("${oss.alibaba.apiKey}")
    String ossApiKey;

    @Value("${oss.alibaba.secretKey}")
    String ossSecretKey;

    @Value("${oss.alibaba.bucketName}")
    String ossBucketName;

    public FileUploadServiceImpl() {
        super(CommonBusinessModules.service);
    }

    private FileUpload client;

    @Override
    @PostConstruct
    public void init() {
        super.init();
        client = new FileUploadClient(ossApiKey, ossSecretKey, ossBucketName);
    }

    @Override
    public void uploadFile(String path, String fileName, File file) throws FileNotFoundException {
        client.uploadFile(path, fileName, file);
    }

    @Override
    public String uploadFile(String path, String fileName, long contentLength, InputStream fileData) {
        return client.uploadFile(path, fileName, contentLength, fileData);
    }

    @Override
    public String getAddress(String key) {
        return client.getAddress(key);
    }

    @Override
    public void downloadFile(String key, File fileDownloadPath) {
        client.downloadFile( key, fileDownloadPath);
    }

    @Override
    public FileContent downloadFile(String key) {
        return client.downloadFile(key);
    }

    @Override
    public List<String> listFile(String path) {
        return client.listFile(path);
    }
}
